package cn.iocoder.yudao.module.water.controller.admin.real;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.water.controller.admin.real.vo.*;
import cn.iocoder.yudao.module.water.dal.dataobject.real.RealDataDO;
import cn.iocoder.yudao.module.water.convert.real.RealDataConvert;
import cn.iocoder.yudao.module.water.service.real.RealDataService;

@Api(tags = "管理后台 - 实时数据")
@RestController
@RequestMapping("/water/real-data")
@Validated
public class RealDataController {

    @Resource
    private RealDataService realDataService;

    @PostMapping("/create")
    @ApiOperation("创建实时数据")
    @PreAuthorize("@ss.hasPermission('water:real-data:create')")
    public CommonResult<Long> createRealData(@Valid @RequestBody RealDataCreateReqVO createReqVO) {
        return success(realDataService.createRealData(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新实时数据")
    @PreAuthorize("@ss.hasPermission('water:real-data:update')")
    public CommonResult<Boolean> updateRealData(@Valid @RequestBody RealDataUpdateReqVO updateReqVO) {
        realDataService.updateRealData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除实时数据")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('water:real-data:delete')")
    public CommonResult<Boolean> deleteRealData(@RequestParam("id") Long id) {
        realDataService.deleteRealData(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得实时数据")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('water:real-data:query')")
    public CommonResult<RealDataRespVO> getRealData(@RequestParam("id") Long id) {
        RealDataDO realData = realDataService.getRealData(id);
        return success(RealDataConvert.INSTANCE.convert(realData));
    }

    @GetMapping("/list")
    @ApiOperation("获得实时数据列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('water:real-data:query')")
    public CommonResult<List<RealDataRespVO>> getRealDataList(@RequestParam("ids") Collection<Long> ids) {
        List<RealDataDO> list = realDataService.getRealDataList(ids);
        return success(RealDataConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得实时数据分页")
    @PreAuthorize("@ss.hasPermission('water:real-data:query')")
    public CommonResult<PageResult<RealDataRespVO>> getRealDataPage(@Valid RealDataPageReqVO pageVO) {
        PageResult<RealDataDO> pageResult = realDataService.getRealDataPage(pageVO);
        return success(RealDataConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出实时数据 Excel")
    @PreAuthorize("@ss.hasPermission('water:real-data:export')")
    @OperateLog(type = EXPORT)
    public void exportRealDataExcel(@Valid RealDataExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<RealDataDO> list = realDataService.getRealDataList(exportReqVO);
        // 导出 Excel
        List<RealDataExcelVO> datas = RealDataConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "实时数据.xls", "数据", RealDataExcelVO.class, datas);
    }

    @GetMapping("/all")
    @ApiOperation("获得全部实时数据列表")
    @PreAuthorize("@ss.hasPermission('water:real-data:query')")
    public CommonResult<List<RealDataRespVO>> getRealDataAllList() {
        List<RealDataDO> list = realDataService.getRealDataAllList();
        return success(RealDataConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/get-value-all-day")
    @ApiOperation("获得今天某值数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tabl", value = "表", required = true, example = "1024", dataTypeClass = String.class),
            @ApiImplicitParam(name = "lable", value = "格", required = true, example = "1024", dataTypeClass = String.class)
    })
    @PreAuthorize("@ss.hasPermission('water:real-data:query')")
    public CommonResult<List<RealDataRespVO>> getRealDataList(@RequestParam("tabl") String tabl, @RequestParam("lable") String lable) {
        List<RealDataDO> list = realDataService.getRealDataList(tabl, lable);
        return success(RealDataConvert.INSTANCE.convertList(list));
    }

}
